1285B - Just Eat It - CodeForces Solution


dp greedy implementation *1300

Please click on ads to support us..

Python Code:

t = int(input())
 
for _ in range(t):
    n = int(input())
    nums = list(map(int, input().split()))
 
 
    def maxsum(arr):
        cur = 0
        ans = -float('inf')

        for num in arr:
            cur += num
            ans = max(ans, cur)

            if cur < 0:
                cur = 0
        
        return ans
    
    res1, res2 = maxsum(nums[1:]), maxsum(nums[:-1])
    if max(res1, res2) >= sum(nums):
        print("NO")
    else:
        print("YES")

    
        

C++ Code:

#include<bits/stdc++.h>
#define INF = INT_MAX >> 1
#define MOD 1000000007
#define int long long
using namespace std;

void solve(){
    int n;
    cin >> n;
    vector<int> input(n, 0);
    for(int i = 0; i < n;i++)
    cin >> input[i];
    int prefix = 0;
    for(int i = 0 ; i <n; i++){
        prefix += input[i];
        if(prefix <= 0){
            cout<<"NO"<<endl;
            return;
        }
    }   
    int suffix = 0;
    for(int i = n - 1; i>=0;i--){
        suffix += input[i];
        if(suffix <= 0){
            cout << "NO" << endl;
            return;
        }
    }
     cout << "YES" << endl;
     return; 
}

int32_t main(){
    ios::sync_with_stdio(0);
    cin.tie(0); 
    int t;
    cin >> t;
    while(t--)
      solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game